/**
 * @author  RR42 <im.razzy@gmail.com>
 * @version 0.1
 *
 * @section LICENSE
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details at
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @section DESCRIPTION
 *
 * This is an utility to run X-programs as root.
 * It controls virtual termainal with su.
 * It isn't su frontend, but uses su.
 * 
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <string.h>
#include <pty.h>
#include "librr42su.h"

#define SU_PROGRAM "su"

char **cmdargs = NULL;

/**
  * This prints help message
  * @author RR42
  */

void rr42xsu_help() {
    g_print("USAGE: rr42xsu [program args]\n"
"\n"
"This program runs program as root. After it did that, it exits,\n"
"and executed program still runs.\n"
"\n"
"This should work: rr42xsu urxvt -e htop (urxvt is X terminal\n"
"emulator, htop is system monitor, \"-e\" is urxvt option). I have\n"
"pointed to urxvt in example because I'm big fan of it.\n"
"\n"
"Please, report bugs to https://code.google.com/p/rr42su/issues/list\n");
}

/**
  * This runs su
  * @author RR42
  *
  * RETURN VALUE: nothing. FIXME
  */

int rr42xsu_run() {
    int fd;
    pid_t pid;

    pid = forkpty(&fd, 0, 0, 0);
    if (pid == 0) { // Forked process goes there:
        if (execvp(cmdargs[0], cmdargs) == -1) {
            //gmessage("ERROR");
        }
    } else if (pid == -1) {
        // Error
        return 1;
    } else { // Parent process goes there
        rr42su_ask_password_and_write(fd);
        
        //FIXME:
        // This is a very messy hack to wait while child process is running
        sleep(1);
        exit(0);
    }
    return 0;
}

int main (int argc, char *argv[]) {
    gtk_init(&argc, &argv);
    if (argc <= 1 ) { //TODO: command line options
        rr42xsu_help();
        return 0;
    }
    
    char **pointer = argv;
    int length = 0;
    //++pointer in while = skip first argument.
    while (*++pointer) length += strlen(*pointer) + 1;
    // 
    cmdargs = (char**)malloc((4)*sizeof(*pointer));
    cmdargs[0] = SU_PROGRAM;
    cmdargs[1] = "-c";
    cmdargs[2] = malloc(length*sizeof(*pointer));
    cmdargs[3] = NULL;

    // Copy all arguments for the execution of su
    pointer = argv;
    char *sourcesymbol, *targetsymbol = &cmdargs[2][0];
    while (*++pointer) {
        sourcesymbol = *pointer;
        while (*sourcesymbol) {
            *targetsymbol = *sourcesymbol;
            targetsymbol++;
            sourcesymbol++;
        }
        *targetsymbol = ' ';
        targetsymbol++;
    }
    cmdargs[2][length-1] = 0; // NULL at end
    
    rr42xsu_run();
    
    return 0;
}
